package com.luoxiang.socialandroid.fragment

import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.angcyo.tablayout.DslTabLayout
import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import com.luoxiang.lib_common.common.Constants.MmkvConst.GENDER
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.base.LazyLoadFragment
import com.luoxiang.socialandroid.databinding.FragmentListBinding
import com.luoxiang.socialandroid.utils.MmkvUtil

/**
 * Author: fuxinbo

 * Date: 2023/2/9

 * Description:
 */

const val domain: String = "https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/"

class ListFragment : LazyLoadFragment() {
    private lateinit var binding: FragmentListBinding

    private val mFragments: MutableList<Fragment> = mutableListOf()

    override fun setContentView(): Int = R.layout.fragment_list

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentListBinding.inflate(layoutInflater)
        super.onCreateView(inflater, container, savedInstanceState)
        return binding.root
    }

    override fun initView() {
        getTabItem(MmkvUtil.decodeInt(GENDER) == 0)
        initViewPage()
    }

    override fun lazyLoad() {

    }

    private fun initViewPage() {
        binding.viewPager.run {
            adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {
                override fun getItemCount(): Int = mFragments.size

                override fun createFragment(position: Int): Fragment = mFragments[position]

            }
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    setViewBg(position)
                }
            })
        }
        ViewPager2Delegate.install(binding.viewPager, binding.tab)
    }

    private fun getTabItem(isWoMan: Boolean = true) {
        binding.tab.addView(TextView(context).apply {
            textSize = 16f
            text = getString(R.string.mi_xml_list_charm)
            gravity = Gravity.CENTER_VERTICAL
            layoutParams = DslTabLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            ).apply {
                rightMargin = 29
            }
        })
        mFragments.add(CharmFragment())
        if (isWoMan) {
            mFragments.add(InviteFragment())
            binding.tab.addView(TextView(context).apply {
                textSize = 16f
                text = getString(R.string.mi_xml_list_invite)
                gravity = Gravity.CENTER_VERTICAL
            })
        }
    }

    private fun setViewBg(index: Int) {
        if (mFragments.size == 1) {
            binding.ivBg.setImageResource(R.mipmap.icon_list_charm_bg)
            return
        }
        binding.ivBg.setImageResource(if (index == 0) R.mipmap.icon_list_charm_bg else R.mipmap.icon_list_invitation_bg)
    }
}